--===========================================================================================================
-- Data insert script for AuthorizationGroup, Role and RoleMapping tables.
--===========================================================================================================

DECLARE @user nvarchar(50) = SYSTEM_USER
DECLARE @now DateTime = GETDATE()

Begin Transaction

	if (select COUNT(*) from RoleMapping) > 0
		truncate table RoleMapping
		
	if (select COUNT(*) from AuthZUserRoleMapping) > 0
		truncate table AuthZUserRoleMapping

	if (select COUNT(*) from Role) > 0
		delete from Role 
		
	if (select COUNT(*) from AuthorizationGroup) > 0
		delete from AuthorizationGroup

	/* Insert the Roles into the Role table.  Set up the parent/child role hierarchy. */
	Insert into Role Values ('SysAdmin', null, @user, @now, NULL, NULL)
	Insert Role select 'Risk Manager', RoleID, @user, @now, NULL, NULL from Role where RoleName = 'SysAdmin'
	Insert Role select 'Task Admin', RoleID, @user, @now, NULL, NULL from Role where RoleName = 'SysAdmin'
	Insert Role select 'Risk User', RoleID, @user, @now, NULL, NULL from Role where RoleName = 'Risk Manager'
	Insert Role select 'Task Manager', RoleID, @user, @now, NULL, NULL from Role where RoleName = 'Risk Manager'
	Insert Role select 'Trusted Task User', RoleID, @user, @now, NULL, NULL from Role where RoleName = 'Task Manager'
	Insert Role select 'Task User', RoleID, @user, @now, NULL, NULL from Role where RoleName = 'Trusted Task User'

Commit Transaction
